package md.NJavaOppo;

import android.Manifest;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import androidx.core.app.ActivityCompat;
import android.util.Log;
import com.heytap.msp.mobad.api.InitParams;
import com.heytap.msp.mobad.api.MobAdManager;
import java.util.ArrayList;
import md.NJavaBase.Debug;
import md.NJavaBase.NJavaBase;
import md.NJavaBase.VideoAdMgr;

public class OppoAd {
    static String Tag = "OppoAd";
    public static final int REQUEST_PERMISSIONS_CODE = 100;

    public static void go() {
        NJavaBase.logOut(Tag, String.format("OppoAd go#1"));


        //监听事件
        NJavaBase.addEventListener(NJavaBase.EVT_REQUEST_PERMISSIONS_RESULT, new OppoOnRequestPermissionsResult());

        NJavaBase.logOut(Tag, String.format("OppoAd go#5"));
        //检查权限
        checkAndRequestPermissions();

        NJavaBase.logOut(Tag, String.format("OppoAd go#6"));
    }

    public static void initSDK() {
        Activity activity = NJavaBase.getActivity();
        String appId = "";
        try {
            ApplicationInfo appInfo = activity.getPackageManager().getApplicationInfo(
                    activity.getPackageName(), PackageManager.GET_META_DATA
            );

            appId = appInfo.metaData.getString("OPPO_AD_APP_ID");
            if (appId.length() > 2 && appId.charAt(0) == 'I' && appId.charAt(1) == 'D')
                appId = appId.substring(2);
        } catch (PackageManager.NameNotFoundException e) {
            NJavaBase.logOut(Tag, "NJavaOppo.go error!");
            e.printStackTrace();
        }

        NJavaBase.logOut(Tag, String.format("OppoAd initSDK#2"));
        //初始化广告SDK
        InitParams initParams = new InitParams.Builder()
                .setDebug(Debug.ENABLE)
                .build();

        NJavaBase.logOut(Tag, String.format("OppoAd initSDK#3"));
        try {
            MobAdManager.getInstance().init(activity, appId, initParams);
        } catch (Exception e) {
            NJavaBase.logOut(Tag, "OppoAd initSDK error!");
            e.printStackTrace();
        }

        NJavaBase.logOut(Tag, String.format("OppoAd initSDK#4"));
        //输出sdk版本号
        NJavaBase.logOut(Tag, String.format("Oppomobad version:%d appid:%s", MobAdManager.getInstance().getSdkVerCode(), appId));


        gInitd = true;
        NJavaBase.logOut(Tag, "OppoAd init end#########！！！！1");

    }

    /**
     * 申请SDK运行需要的权限
     * 注意：READ_PHONE_STATE、WRITE_EXTERNAL_STORAGE 两个权限是必须权限，没有这两个权限SDK无法正常获得广告。
     * WRITE_CALENDAR、ACCESS_FINE_LOCATION 是两个可选权限；没有不影响SDK获取广告；但是如果应用申请到该权限，会显著提升应用的广告收益。
     */
    /*
    static void checkAndRequestPermissions() {
        Activity activity = NJavaBase.getActivity();
        ArrayList<String> mNeedRequestPMSList = new  ArrayList<String>();
         //READ_PHONE_STATE、WRITE_EXTERNAL_STORAGE 两个权限是必须权限，没有这两个权限SDK无法正常获得广告。
        if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE)) {
            mNeedRequestPMSList.add(Manifest.permission.READ_PHONE_STATE);
        }
        if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            mNeedRequestPMSList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }

         //WRITE_CALENDAR、ACCESS_FINE_LOCATION 是两个可选权限；没有不影响SDK获取广告；但是如果应用申请到该权限，会显著提升应用的广告收益。
        if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_CALENDAR)) {
            mNeedRequestPMSList.add(Manifest.permission.WRITE_CALENDAR);
        }
        if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION)) {
            mNeedRequestPMSList.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }


        if (0 == mNeedRequestPMSList.size()) {

             //权限都已经有了，那么直接调用SDK请求广告。

            //fetchSplashAd();
            initSDK();
        } else {

             //有权限需要申请，主动申请。

            String[] temp = new String[mNeedRequestPMSList.size()];
            mNeedRequestPMSList.toArray(temp);
            ActivityCompat.requestPermissions(activity, temp, REQUEST_PERMISSIONS_CODE);
        }
    }*/
    static void checkAndRequestPermissions() {
        Activity activity = NJavaBase.getActivity();
        ArrayList<String> mNeedRequestPMSList = new ArrayList<String>();

        /** * Android Q以下READ_PHONE_STATE 权限是必须权限，没有这个权限SDK无法正常获得广告。 */
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q &&
                PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE)) {
            mNeedRequestPMSList.add(Manifest.permission.READ_PHONE_STATE);
        }

        /** * WRITE_EXTERNAL_STORAGE、ACCESS_FINE_LOCATION 是两个可选权限；没有不影响SDK获取广告；但是如果应用申请到该权限，会显著提升应用的广告收益。 */
        if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            mNeedRequestPMSList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }

        if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION)) {
            mNeedRequestPMSList.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }

        if (0 == mNeedRequestPMSList.size()) {
            /** * 权限都已经有了，那么直接调用SDK请求广告。 */
            NJavaBase.logOut(Tag,"已有权限，直接初始化");
            initSDK();
        } else {
            NJavaBase.logOut(Tag,"没有权限，申请权限");
            /** * 有权限需要申请，主动申请。 */
            String[] temp = new String[mNeedRequestPMSList.size()];
            mNeedRequestPMSList.toArray(temp);
            ActivityCompat.requestPermissions(activity, temp, REQUEST_PERMISSIONS_CODE);
        }

        NJavaBase.logOut(Tag,"初始化完成");

    }


    /**
     * 判断应用是否已经获得SDK运行必须的READ_PHONE_STATE、WRITE_EXTERNAL_STORAGE两个权限。
     *
     * @return
     *//*
    public static boolean hasNecessaryPMSGranted() {
        Activity activity = NJavaBase.getActivity();
        if (PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE)) {
            if (PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                return true;
            }
        }
        return false;
    }*/
    public static boolean hasNecessaryPMSGranted() {
        Activity activity = NJavaBase.getActivity();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ||
                PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE))
        { return true; }

        return false;
    }

    public static boolean isInited(){
        return gInitd;
    }


    static boolean gInitd =false;
}
